/**
 * 闲聊工具
 * Created by dabai on 2017/8/30.
 */
var XianLiaoUtil = {};
/**
 * 是否安装
 * @return {Boolean}
 * @public
 */
XianLiaoUtil.isInstalled = function () {
    if (cc.sys.os == cc.sys.OS_IOS) {
        return jsb.reflection.callStaticMethod('XianLiaoUtil', 'isInstalled');
    } else if (cc.sys.os == cc.sys.OS_ANDROID) {
        return jsb.reflection.callStaticMethod(packageUri + '/utils/XianLiaoUtil', 'isInstalled', '()Z');
    }
};
/**
 * 拉起登录
 * @public
 */
XianLiaoUtil.redirectLogin = function () {
    if (cc.sys.os == cc.sys.OS_IOS) {
        jsb.reflection.callStaticMethod('XianLiaoUtil', 'redirectLogin');
    } else if (cc.sys.os == cc.sys.OS_ANDROID) {
        jsb.reflection.callStaticMethod(packageUri + '/utils/XianLiaoUtil', 'redirectLogin', '()V');
    }
};
/**
 * 登录回调
 * @param {String} code
 * @param state
 * @public
 */
XianLiaoUtil.loginResq = function (code, state) {
    if (state && state != 0) {
        return;
    }
    eventManager.dispatchEvent(EventKey.ReceiveXianliaoCode, code);
};

/**
 * 分享回调
 * @param {String} code
 * @public
 */
XianLiaoUtil.shareResq = function (code) {
    eventManager.dispatchEvent(EventKey.ReceiveXianliaoShare, code);
};
/**
 * 分享游戏
 * @param {String} roomId
 * @param {String} title
 * @param {String} text
 * @public
 */
XianLiaoUtil.shareGame = function (roomId, title, text) {
    roomId = roomId.toString();
    if (cc.sys.os == cc.sys.OS_IOS) {
        jsb.reflection.callStaticMethod(
            'XianLiaoUtil',
            'shareGame:title:text:',
            roomId, title, text
        );
    } else if (cc.sys.os == cc.sys.OS_ANDROID) {
        jsb.reflection.callStaticMethod(
            packageUri + '/utils/XianLiaoUtil',
            'shareGame', '(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V',
            roomId, title, text
        );
    }
};
/**
 * 分享游戏回调
 * @param {String} roomToken
 * @param {String} roomId
 * @param {String} openId
 * @public
 */
XianLiaoUtil.getGameResq = function (roomToken, roomId, openId) {
    var data = {
        roomToken: roomToken,
        roomId: roomId,
        openId: openId
    };
    this.GameResqData = data;
    // cc.log('roomToken=====' + roomToken);
    // cc.log('roomId=====' + roomId);
    // cc.log('openId=====' + openId);
    eventManager.dispatchEvent(EventKey.ReceiveXianliaoShareGame, data);
};


/**
 * 取闲聊房间ID返回
 * @return {String}
 */
XianLiaoUtil.getReturnId = function () {
    if (this.GameResqData) {
        if (this.GameResqData.roomId) {
            return this.GameResqData.roomId;
        }
        if (this.GameResqData.roomToken) {
            return this.GameResqData.roomToken.split(',')[0];
        }
    }
    return null
};
/**
 * 清除魔窗返回
 */
XianLiaoUtil.clearReturnId = function () {
    this.GameResqData = null;
};
/**
 * 分享图片
 * @param {String} path
 * @public
 */
XianLiaoUtil.sharePic = function (path) {
    if (cc.sys.os == cc.sys.OS_IOS) {
        jsb.reflection.callStaticMethod(
            'XianLiaoUtil',
            'sharePic:',
            path
        );
    } else if (cc.sys.os == cc.sys.OS_ANDROID) {
        jsb.reflection.callStaticMethod(
            packageUri + '/utils/XianLiaoUtil',
            'sharePic', '(Ljava/lang/String;)V',
            path
        );
    }
};
/**
 * 截图
 * @param {cc.Node} node
 * @public
 */
XianLiaoUtil.shareCapture = function (node) {
    captureNode(node, function (path) {
        XianLiaoUtil.sharePic(path);
    });
};
/**
 * 分享文本
 * @param {String} text
 */
XianLiaoUtil.shareText = function (text) {
    if (cc.sys.os == cc.sys.OS_IOS) {
        jsb.reflection.callStaticMethod(
            'XianLiaoUtil',
            'shareText:',
            text
        );
    } else if (cc.sys.os == cc.sys.OS_ANDROID) {
        jsb.reflection.callStaticMethod(
            packageUri + '/utils/XianLiaoUtil',
            'shareText', '(Ljava/lang/String;)V',
            text
        );
    }
};
